package com.wzy.reggie.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.wzy.reggie.common.BaseContext;
import com.wzy.reggie.common.R;
import com.wzy.reggie.entity.ShoppingCart;
import com.wzy.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
@Slf4j
public class ShoppingCartController {
    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 获取购物车数据
     * @param
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list(){
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
        queryWrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
        return R.success(list);
    }
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("购物车数据：{}",shoppingCart);
        //设置用户id 指定当前是哪个用户的购物车数据
        Long currentId = BaseContext.getCurrentId();
        shoppingCart.setUserId(currentId);
        //查询当前菜品是否在购物车中
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        if(dishId!=null){
            //添加到购物车的是菜品
            queryWrapper.eq(ShoppingCart::getDishId,dishId);
        }else{
            //否则是套餐
            queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
        }
        //如果已经存在，就在原先数量基础上加一
        //SQL:select * from shopping_cart where user_id =? and dish_id=?/setmeal_id=?
        ShoppingCart cartServiceOne = shoppingCartService.getOne(queryWrapper);

        if(cartServiceOne != null){
            //如果已经存在 就在原来数量基础上加一
            Integer number = cartServiceOne.getNumber();
            cartServiceOne.setNumber(number+1);
            shoppingCartService.updateById(cartServiceOne);
        }else{
            //如果不存在，则添加到购物车，数量默认为1
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCartService.save(shoppingCart);
            cartServiceOne=shoppingCart;
        }
        return R.success(cartServiceOne);
    }

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> clean(){
        Long currentId = BaseContext.getCurrentId();
        LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(ShoppingCart::getUserId,currentId);
        shoppingCartService.remove(queryWrapper);
        return R.success("清空购物车成功");
    }

    /**
     * 减购物车商品
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart){
        //前端传入json对象 首先先判断是菜品还是套餐 根据菜品id或者套餐id从数据库中删除
            Long dishId = shoppingCart.getDishId();
            if(dishId!=null){
                //dish_id不为空 此时为菜品 根据菜品id查出当前菜品
                LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
                queryWrapper.eq(ShoppingCart::getDishId,dishId);
                ShoppingCart dishOne = shoppingCartService.getOne(queryWrapper);
                //此时需要判断当前菜品的数量是否为1
                Integer number = dishOne.getNumber();
                if(number==1){
                    //当number为1 时直接从数据库中删除
                    shoppingCartService.removeById(dishOne.getId());
                }else{
                    dishOne.setNumber(number-1);
                    shoppingCartService.updateById(dishOne);
                }

            }else{
                //dish_id为空 此时为套餐 根据套餐id查出当前套餐
                LambdaQueryWrapper<ShoppingCart> queryWrapper = new LambdaQueryWrapper<>();
                queryWrapper.eq(ShoppingCart::getUserId,BaseContext.getCurrentId());
                queryWrapper.eq(ShoppingCart::getSetmealId,shoppingCart.getSetmealId());
                ShoppingCart dishOne = shoppingCartService.getOne(queryWrapper);
                //此时需要判断当前套餐的数量是否为1
                Integer number = dishOne.getNumber();
                if(number==1){
                    //当number为1 时直接从数据库中删除
                    shoppingCartService.removeById(dishOne.getId());
                }else{
                    dishOne.setNumber(number-1);
                    shoppingCartService.updateById(dishOne);
                }
            }
            return R.success("删减成功");
    }
}
